<template>
  <div class="app-container">
    <!-- 搜索 -->
    <el-form ref="searchForm" :inline="true" :model="searchForm" class="demo-form-inline">
      <el-form-item label="税率类型；">
        <el-select v-model="searchForm.discountName" placeholder="请选择税率类型：">
          <el-option label="折现率" value="折现率" />
        </el-select>
      </el-form-item>
      <el-form-item label="是否开启：">
        <el-select v-model="searchForm.status" placeholder="是否开启">
          <el-option label="开启" value="0" />
          <el-option label="关闭" value="1" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="searchBtn">查询</el-button>
        <el-button type="info" icon="el-icon-refresh" @click="restBtn">重置</el-button>
      </el-form-item>
    </el-form>
    <!-- 按钮组 -->
    <el-row>
      <el-button type="primary" size="mini" icon="el-icon-plus" plain @click="addDiscountDialog">新增</el-button>
    </el-row>
    <!-- 表格 -->
    <!-- 表格 -->
    <el-table v-loading="loading" :data="discountList">

      <el-table-column label="税率类型" prop="discountType" />
      <el-table-column label="税率(年)/%" prop="taxRate" :show-overflow-tooltip="true" />
      <el-table-column label="生效日期" prop="startTime" :show-overflow-tooltip="true" />
      <el-table-column label="截止日期" prop="endTime" :show-overflow-tooltip="true" />
      <el-table-column label="适用期限" prop="term" :show-overflow-tooltip="true" />
      <el-table-column label="是否开启">
        <template slot-scope="scope">
          <el-switch v-model="scope.row.status" active-value="0" inactive-value="1" @change="updateStatus(scope.row)" />
        </template>
      </el-table-column>
      <el-table-column label="最后一次操作时间" prop="lastTime" :show-overflow-tooltip="true" />
      <el-table-column label="操作人" prop="operator" :show-overflow-tooltip="true" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="200">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleEdit(scope.row)"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handlerDelete(scope.row)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- dialog -->
    <el-dialog
      title="添加角色"
      :visible.sync="addDiscountVisible"
      width="50%"
    >
      <el-form ref="addDiscountForm" label-width="80px" :model="addDiscountForm" :rules="addDiscountRules">
        <el-form-item label="税率类型" prop="discountType">
          <el-select v-model="addDiscountForm.discountType" placeholder="请选择税率类型" style="width:100%" size="mini">
            <el-option label="折现率" value="折现率" />
          </el-select>
        </el-form-item>
        <el-form-item label="税率" prop="taxRate">
          <el-input v-model="addDiscountForm.taxRate" size="small" placeholder="请输入权限字符" />
        </el-form-item>
        <el-form-item label="开始时间" prop="startTime">
          <el-date-picker
            v-model="addDiscountForm.startTime"
            style="width:100%"
            align="center"
            type="date"
            value-format="yyyy-MM-dd"
            size="mini"
            placeholder="选择日期"
          />
        </el-form-item>
        <el-form-item label="结束时间" prop="endTime">
          <el-date-picker
            v-model="addDiscountForm.endTime"
            style="width:100%"
            align="center"
            type="date"
            value-format="yyyy-MM-dd"
            size="mini"
            placeholder="选择日期"
          />
        </el-form-item>
        <el-form-item label="适用期限" prop="term">
          <el-select v-model="addDiscountForm.term" placeholder="适用期限" style="width:100%" size="mini">
            <el-option label="1-2年" value="1-2年" />
            <el-option label="2-3年" value="2-3年" />
            <el-option label="3-5年" value="3-5年" />
          </el-select>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button size="small" @click="addUserVisible = false">取 消</el-button>
        <el-button type="primary" size="small" @click="addDiscountHandler">添 加</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { getDiscount, updateDiscount, deleteDiscount, addDiscount } from '@/api/discount'

export default {
  name: 'UiDiscount',
  data() {
    return {
      // 搜索条件
      searchForm: {
        discountName: '',
        status: ''
      },
      // 数据列表
      discountList: [],
      // 表格加载
      loading: true,
      // 添加角色的dialog
      addDiscountVisible: false,
      // 添加角色的表单数据
      addDiscountForm: {
        discountType: '',
        taxRate: '',
        startTime: '',
        endTime: '',
        term: ''
      },
      // 添加角色的表单验证规则
      addDiscountRules: {
        startTime: [
          { required: true, message: '请选择开始日期', trigger: 'blur' }
        ],
        endTime: [
          { required: true, message: '请选择结束日期', trigger: 'blur' }
        ],
        discountType: [
          { required: true, message: '请选择税率类型', trigger: 'blur' }
        ],
        taxRate: [
          { required: true, message: '请输入税率', trigger: 'blur' }
        ],
        term: [
          { required: true, message: '请选择适用期限', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    // 初始化数据
    this.getDiscountData()
  },
  methods: {
    // 后端信息获取
    getDiscountData() {
      getDiscount(this.searchForm).then(res => {
        const { data } = res
        this.discountList = data
        this.loading = false
      })
    },
    // 搜索按钮
    searchBtn() {
      this.getDiscountData()
    },
    // 重置表单
    restBtn() {
      this.searchForm.discountName = ''
      this.searchForm.status = ''
      this.getDiscountData()
    },
    // 修改状态值
    updateStatus(row) {
      updateDiscount({ id: row.id, status: row.status }).then(res => {
        const { msg } = res
        this.$message({ message: msg, type: 'success' })
        this.getDiscountData()
      }).catch(() => {
        this.getDiscountData()
      })
    },
    // 删除折现率
    handlerDelete(data) {
      this.$confirm('是否永久删除此数据？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteDiscount(data.id).then(res => {
          const { msg } = res
          this.$message({ message: msg, type: 'success' })
          this.getDiscountData()
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    // 打开添加的dialog
    addDiscountDialog() {
      this.addDiscountVisible = true
    },
    // 添加折现率信息
    addDiscountHandler() {
      this.$refs['addDiscountForm'].validate((valid) => {
        if (valid) {
          console.log(this.addDiscountForm)
          addDiscount(this.addDiscountForm).then(res => {
            console.log(res)
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
